﻿using System;
using System.Diagnostics;
using Medianamik.Tests.Common;
using NUnit.Framework;
using System.Globalization;

namespace Medianamik.Core
{
    [TestFixture]
    [Ignore]
    public class NodeManagerTest
    {
        private static readonly Guid SimpleTypeId = new Guid("a0af1a7a-2bff-48e6-89b1-e4011d897185");
        private static readonly CultureInfo Culture = new CultureInfo("fr-CA");
        private static readonly Guid ComplexTypeId = new Guid("60f43c3a-8222-416d-8c2f-356c7db45935");

        [TestFixtureTearDown]
        public void TearDown()
        {
            Utilities.RestoreDatabase("RetoreCS");
        }

        [Test]
        public void Time_required_for_saving_simple_node()
        {
            var timeWatcher = new Stopwatch();

            SaveNode(timeWatcher, SimpleTypeId);

            Assert.That(timeWatcher.ElapsedMilliseconds, Is.LessThan(10));
        }

        [Test]
        public void Average_time_required_for_saving_simple_node()
        {
            var timeWatcher = new Stopwatch();
            var nbOfInsertion = 200;

            for (int i = 0; i < nbOfInsertion; i++)
                SaveNode(timeWatcher, SimpleTypeId);

            Assert.That(timeWatcher.ElapsedMilliseconds / nbOfInsertion, Is.LessThan(10));
        }

        [Test]
        public void Time_required_for_saving_complex_node()
        {
            var timeWatcher = new Stopwatch();

            SaveNode(timeWatcher, ComplexTypeId);

            Assert.That(timeWatcher.ElapsedMilliseconds, Is.LessThan(550));
        }

        [Test]
        public void Average_time_required_for_saving_complex_nodes()
        {
            var timeWatcher = new Stopwatch();
            var nbOfInsertion = 200;

            for (int i = 0; i < nbOfInsertion; i++)
                SaveNode(timeWatcher, ComplexTypeId);

            Assert.That(timeWatcher.ElapsedMilliseconds / nbOfInsertion, Is.LessThan(10));
        }

        private void SaveNode(Stopwatch timeWatcher, Guid nodeId)
        {
            var node = NodeManager.NewNode(nodeId, Culture);

            //node.AddNodeInstance(Culture.Name);
            node.DefaultCulture = Culture;
            node.DefaultInstance.Properties["Name"].SetValue("SimpleNode");

            timeWatcher.Start();
            NodeManager.SaveNode(node);
            timeWatcher.Stop();
        }
    }
}
